﻿using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Web;
using System.Web.Http;
using WebApi.Services;

namespace WebApi.Controllers
{
    [RoutePrefix("api/Admin/Goods")]
    public class GoodsController : ApiController
    {
      
        //[Route("AddGoods"), HttpPost]
        //public string AddGoods()
        //{
        //    var req = HttpContext.Current.Request;
        //    MyGoods.CreateGoods(req["pro_name"], req["pro_type"], req["pro_unit"], req["pro_label1"], req["pro_label2"], int.Parse(req["pro_rank"]), int.Parse(req["pro_state"]), req["pro_activity"], decimal.Parse(req["pro_ratio"]), decimal.Parse(req["pro_dprice"]), req["pro_describe"], req["pro_pic"]);
        //    return "";
        //}


        [Route("GoodInfo"), HttpGet]
        public JObject GoodInfo()
        {
            var req = HttpContext.Current.Request;
             return MyGoods.GetGoods(int.Parse(req["pro_id"]));
           
        }


        [Route("productlist"), HttpGet]
        public JArray GoodsList()
        {
            return MyGoods.GoodsList();
        }

        [Route("ChangeGoods"), HttpGet]
        public Boolean ChangeGood()
        {
            var req = HttpContext.Current.Request;
            MyGoods.ChangeGoods(int.Parse(req["pro_id"]), req["pro_name"], req["pro_type"], req["pro_unit"], req["pro_label1"], req["pro_label2"],int.Parse(req["pro_rank"]),int.Parse(req["pro_state"]), req["pro_activity"], decimal.Parse(req["pro_ratio"]),decimal.Parse( req["pro_dprice"]), req["pro_describe"], req["pro_pic"]);
            return true;
        }

        [Route("DeleteGoods"), HttpGet]
        public Boolean DeleteGoods()
        {
            var req = HttpContext.Current.Request;
            return MyGoods.DeleteGoods(int.Parse(req["pro_id"]));
        }


        [Route("AddPic"), HttpPost]
        public string AddPic()
        {
            var req = HttpContext.Current.Request;
            HttpFileCollection pics = HttpContext.Current.Request.Files; //接收前端发来的图片   
            if (pics.Count <= 0) return "no picture";
            var picpath = MyGoods.upLoadPics(pics[0]);
            var scrollpath = MyGoods.upLoadPics(pics);
            MyGoods.CreateGoods(req["pro_name"], req["pro_type"], req["pro_unit"], req["pro_label1"], req["pro_label2"], int.Parse(req["pro_rank"]), int.Parse(req["pro_state"]), req["pro_activity"], decimal.Parse(req["pro_ratio"]), decimal.Parse(req["pro_dprice"]), req["pro_describe"], picpath, scrollpath);



            return "";
        }

        //模糊查询
        [Route("GoodsrFuzzy"), HttpGet]
        public JArray CustomerFuzzy()
        {
            var req = HttpContext.Current.Request;
            return MyGoods.FuzzySearch(req["searchText"]);
        }

    }
}


